package com.task.sql.schedule.worker;/**
 * @ClassName : GenerateTaskWorker
 * @Description : 生成任务定时任务-每天执行一次
 * @Author : liudonghai
 * @Date: 2024-09-11 14:20
 */

import ch.qos.logback.core.util.StringCollectionUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.task.sql.schedule.constant.DBColumnValue;
import com.task.sql.schedule.domain.entity.SqlTask;
import com.task.sql.schedule.domain.entity.SqlTaskSchedule;
import com.task.sql.schedule.domain.vo.SqlTaskVO;
import com.task.sql.schedule.service.SqlTaskScheduleService;
import com.task.sql.schedule.service.SqlTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.List;

/**
 * @ClassName : GenerateTaskWorker
 * @Description : 生成任务定时任务-每天执行一次
 * @Author : liudonghai
 * @Date: 2024-09-11 14:20
 */
@Slf4j
@Component
public class GenerateTaskWorker {
    @Resource
    private SqlTaskScheduleService sqlTaskScheduleService;
    @Resource
    private SqlTaskService sqlTaskService;

    @Scheduled(cron = "${worker.generate.cron}")
    public void task(){
        System.out.println("------------generate start-----------");
        List<SqlTask> sqlTaskVOS = sqlTaskService.listPendingTasks();
        if(CollectionUtil.isEmpty(sqlTaskVOS)){
            log.info("生成待处理的任务worker未获取到任何任务基础数据");
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDate = now.with(LocalTime.MIN);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String nowFormatter = now.format(dateTimeFormatter);
        Integer nowDate = Integer.parseInt(nowFormatter);
        for(SqlTask vo:sqlTaskVOS){
            List<SqlTaskSchedule> list = sqlTaskScheduleService.query()
                    .eq("task_no", vo.getId()).eq("insert_date", nowDate).list();
            if(CollectionUtil.isNotEmpty(list)){
                log.info("今日待执行的任务已经生成过，不再重复生成");
                continue;
            }
            SqlTaskSchedule scheduleTask = generate(vo);
            scheduleTask.setInsertDate(nowDate);
            boolean save = sqlTaskScheduleService.save(scheduleTask);
            log.info(String.format("为任务基础数据%s生成可执行任务结果：%s", JSON.toJSONString(vo),save?"success":"failed"));
        }
        System.out.println("------------generate end-----------");
    }

    private SqlTaskSchedule generate(SqlTask vo){
        Calendar instance = Calendar.getInstance();
        SqlTaskSchedule scheduleTask = new SqlTaskSchedule();
        scheduleTask.setTaskNo(vo.getId());
        scheduleTask.setPreTask(StringUtils.isEmpty(vo.getPreTaskNo())?0:1);
        scheduleTask.setNum(0);
        scheduleTask.setFailedNum(0);
        scheduleTask.setExecState(DBColumnValue.EXEC_STATE_0.getCode());
        scheduleTask.setExecAddr("");
        scheduleTask.setExecDesc("");
        scheduleTask.setExecHandler("");
        scheduleTask.setYn(DBColumnValue.YN_1.getCode());
        scheduleTask.setInsertTime(instance.getTime());
        scheduleTask.setInsertUser(DBColumnValue.CREATE_USER_1.getMsg());
        scheduleTask.setUpdateTime(instance.getTime());
        scheduleTask.setUpdateUser(DBColumnValue.CREATE_USER_1.getMsg());
        return scheduleTask;
    }

}
